<?php
// +----------------------------------------------------------------------
// | 数据库迁移 - 创建访问数据库
// +----------------------------------------------------------------------

use think\migration\Migrator;

class CreateAccess extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {

        // TOKEN认证表
        $table = $this->table('access_tokens', ['engine' => 'InnerDB', 'collation' => 'utf8mb4_bin', 'comment' => 'TOKEN认证表']);
        $table->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '类型'])
            ->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('salt', 'string', ['limit' => 32, 'default' => NULL, 'comment' => '加盐参数'])
            ->addColumn('token', 'string', ['limit' => 32, 'default' => NULL, 'comment' => 'TOKEN'])
            ->addColumn('expire_time', 'datetime', ['default' => NULL, 'comment' => '有效期至'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->create();

        // 短信认证表
        $smsTable = $this->table('access_sms', ['engine' => 'InnerDB', 'collation' => 'utf8mb4_bin', 'comment' => '短信认证表']);
        $smsTable->addColumn('phone_number', 'string', ['limit' => 15, 'null' => false, 'comment' => '手机号'])
            ->addColumn('code', 'string', ['limit' => 10, 'null' => false, 'comment' => '验证码'])
            ->addColumn('is_used', 'boolean', ['limit' => 1, 'default' => 0, 'comment' => '是否已使用'])
            ->addColumn('send_time', 'datetime', ['default' => NULL, 'comment' => '发送时间'])
            ->addColumn('expire_time', 'datetime', ['default' => NULL, 'comment' => '有效期至'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->addIndex('phone_number')
            ->addIndex(['phone_number', 'code', 'is_used'])
            ->create();
    }
}
